﻿Imports System.Text
Imports System.ComponentModel

Public Class Generator
    Implements INotifyPropertyChanged
    Private list As New StringBuilder

    Public Event PropertyChanged As PropertyChangedEventHandler Implements INotifyPropertyChanged.PropertyChanged

    'return index of number
    Private Function getNumber() ' return an integer in the range of 48 - 57 (0-9 ASCii)
        Randomize()
        Dim num As Integer
        num = Math.Floor((57 - 48 + 1) * Rnd()) + 48
        Return num
    End Function

    'return index of symbol
    Private Function getSymbol() 'ASCii ranges (33-47), (58 - 64), (123-126)
        Dim symbol As Integer
        Dim selector As Integer
        Randomize()

        selector = (3 * Rnd()) + 1 'select lower case or uppercase

        If selector = 1 Then
            symbol = Math.Floor((47 - 33 + 1) * Rnd()) + 33
        ElseIf selector = 2 Then
            symbol = Math.Floor((64 - 58 + 1) * Rnd()) + 58
        Else
            symbol = Math.Floor((126 - 123 + 1) * Rnd()) + 123
        End If

        Return symbol
    End Function

    'return index of letter
    Private Function getLetter() 'ASCii ranges upper case = (65-90), lower case = (97 - 122)
        Dim letter As Integer
        Dim selector As Integer
        Randomize()

        selector = (2 * Rnd()) + 1 'select lower case or uppercase

        If selector = 1 Then
            letter = Math.Floor((90 - 65 + 1) * Rnd()) + 65
        Else
            letter = Math.Floor((122 - 97 + 1) * Rnd()) + 97
        End If

        Return letter
    End Function

    Public Sub getRandom(ByVal limit, ByVal letters, ByVal numbers, ByVal symbols, ByVal clip)
        Dim rand As Integer
        Dim counter = 0
        Dim itemAdded As Boolean = False
        Randomize()

        While counter < limit
            rand = (3 * Rnd()) + 1 'determins next char in password (number, letter, or symbol)

            If numbers = True And rand = 1 Then 'add number to list
                Password.Append(Chr(getNumber()))
                itemAdded = True
                counter += 1
            ElseIf letters = True And rand <= 2 Then 'add letter to list
                Password.Append(Chr(getLetter()))
                itemAdded = True
                counter += 1
            ElseIf symbols = True And rand <= 3 Then 'add symbol to list
                Password.Append(Chr(getSymbol()))
                itemAdded = True
                counter += 1
            End If
            itemAdded = False
        End While
        If clip = True Then
            My.Computer.Clipboard.SetText(Password.ToString)
        End If


    End Sub

    Protected Sub OnPropertyChanged(ByVal name As String)
        RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(name))
    End Sub

    Public Property Password() As StringBuilder
        Get
            Return list
        End Get
        Set(ByVal value As StringBuilder)
            list = value
            OnPropertyChanged("Password")
        End Set
    End Property
End Class
